<template>
  <div class="about">
    <!-- 添加 -->
    <p style="text-align: left">
      <el-button type="primary" @click="dialogFormAdd = true">添加</el-button>
    </p>
    <el-dialog title="添加人员" :visible.sync="dialogFormAdd">
      <el-form :model="form">
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="form.header" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="有效" value="1"></el-option>
            <el-option label="无效" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormAdd = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180"> </el-table-column>
      <el-table-column prop="nickName" label="昵称" width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="header" label="头像" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="info">有效</el-tag>
          <el-tag v-if="scope.row.status == 0" type="info">无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="50"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],

      dialogFormAdd: false,
      formLabelWidth: "120px",
      form: {
        nickName: "",
        name: "",
        email: "",
        status: null,
      },
    };
  },

  created() {
    this.query();
  },

  methods: {
    query() {
      this.axios.get("http://localhost:3000/users").then((Response) => {
        this.tableData = Response.data;
      });
    },

    //删除
    handleDelete(index, row) {
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() =>
        this.axios
          .delete("http://localhost:3000/users/" + row.id)
          .then((Response) => {
            this.query();
          })
      );
    },

    handleEdit(index, row) {},

    //添加
    add(form) {
      this.axios({
        method:'post',
        url:"http://localhost:3000/users",
         headers:{
                'Content-Type':'application/json;charset=utf-8'      //改这里就好了
          },
          params:this.form,
      })
     
        .then((Response) => {
          console.log(this.form);
          this.dialogFormAdd = false;
          this.query();
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //分页
    handleCurrentChange(page) {},
  },

  mounted(){
    this.query()
  }
};
</script>

<style scoped>
</style>
